///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

import flash.events.StatusEvent;

import mx.controls.TextInput;
import mx.events.DataGridEvent;

import navigator.Helpers;
import navigator.State;

import summit.basecamp.constants.BasecampResource;
import summit.basecamp.wrappers.AbstractWrapper;
import summit.basecamp.wrappers.CommentWrapper;
import summit.basecamp.wrappers.IStandardWrapper;

import summit.events.BasecampAPIEvent;

import navigator.BasecampAPIEventHandlers;

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

/** XML Fragment For Property View XML **/
[Bindable]
public var dataGridXML:XML =
<items>
	<item/>
</items>;

/** @private Label Text **/
public static const SELECTLABEL:String = "Select a Type";

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

/**
 * Called by Navigator at application complete.
 */
public function wireEvents():void
{
	State.tree.addEventListener(StatusEvent.STATUS, handleTreeStatusEvent);	
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

/** @private Called when the ResourceTree dispatches a StatusEvent. **/
private function handleTreeStatusEvent(e:StatusEvent):void
{
	if(e.code != "click") return;
	
	var item:XML;
	var entry:XML = (State.tree.selectedItem as XML);
	
	var abstractWrapper:AbstractWrapper;
	
	if(e.level != "node") // clicked on 'nothing' case...
	{
		Helpers.resetPropertyView();
		return;
	}
	
	item = State.resources[entry.@id];
	abstractWrapper = Helpers.getClass(entry.@resource);
	
	createLabel.visible = Helpers.getCreateLabelStatus(abstractWrapper, entry);
	
	if(null == item)
	{	
		Helpers.resetPropertyView();
		return;
	}
	
	delete this.dataGridXML.item;
	this.typeLabel.text = entry.@apiType.toString().toUpperCase();
	item.setLocalName("item");
	dataGridXML.item = item;
	
	Helpers.configureToolBar(abstractWrapper);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Standard UI Event Handlers

/** @private Called when a user clicks 'Create' for a type of item. */
private function handleCreateClickEvent(e:MouseEvent):void
{
	var node:XML = (State.tree.selectedItem as XML);
	var parentXML:XML = node.parent();
	var abstractWrapper:AbstractWrapper;
	var parentId:String;
	
	// Special case - if a todo-list (not the folder) is selected
	// and create is click - we need to handle the call as TodoItem
	// resource.
	if(node.@resource == "TodoList" && node.@id != -2)
	{
		abstractWrapper = Helpers.getClass("TodoItem");
		parentId = node.@id;
	}
	else 
	{
		abstractWrapper = Helpers.getClass(node.@resource);
		parentId = parentXML.@id;
	}
	
	var stdWrapper:IStandardWrapper = abstractWrapper as IStandardWrapper;
	Helpers.registerCommonEvents(stdWrapper.loader);
	
	if(createLabel.text == "Create") // The user clicked 'Create' 
	{
		stdWrapper.loader.addEventListener(BasecampAPIEvent.OK, 
		BasecampAPIEventHandlers.handlePrepareOKEvent);
		
		stdWrapper.prepare(parentId);
	}
	else
	{	// doing create needs a little customization depending on type
		var xml:XML = new XML(this.dataGridXML.item);
		xml.setLocalName(abstractWrapper.resourceType);
	
		var newItem:XML;
		
		if(abstractWrapper.resourceType == BasecampResource.MESSAGE ||
		abstractWrapper.resourceType == BasecampResource.MILESTONE)
		{
			newItem = new XML("<request/>");
			newItem.appendChild(xml);
		}
		else newItem = new XML(xml);
		
		stdWrapper.loader.addEventListener(BasecampAPIEvent.OK,
		BasecampAPIEventHandlers.handleCreateOKEvent);
		
		stdWrapper.create(newItem);
	}
}

/**
 * @private
 * 
 * Called when a user clicks on the edit label - which displays 'Edit' or 
 * 'Submit Edit', depending on if an edit is ready to be submitted.
 */
private function handleEditClickEvent(e:MouseEvent):void
{
	var node:XML = State.tree.selectedItem as XML; // currently selected item

	var abstractWrapper:AbstractWrapper = Helpers.getClass(node.@resource);
	Helpers.registerCommonEvents(abstractWrapper.loader);
	
	if(editLabel.text == "Submit Edit") 
	{
		// The user was previously editing an item - now is ready to submit
		// the changes to the server. 
		editColumn.editable = false;				// turn off editing
		editColumn.setStyle("color", "0x000000");
		
		var editedXML:XML = new XML(dataGridXML.item);	// get the edited XML
		editedXML.setLocalName(abstractWrapper.resourceType);
		
		abstractWrapper.loader.addEventListener(BasecampAPIEvent.OK,
		BasecampAPIEventHandlers.handleUpdateOKEvent);
		
		if(abstractWrapper.resourceType == BasecampResource.MILESTONE)
		{
			var newItem:XML = new XML("<request/>");
			newItem.appendChild(editedXML);
			editedXML = newItem;
		}
		
		var stdWrapper:IStandardWrapper = abstractWrapper as IStandardWrapper;
		if(stdWrapper) stdWrapper.update(editedXML);
		else (abstractWrapper as CommentWrapper).update(editedXML);
	}
	else
	{
		abstractWrapper.loader.addEventListener(BasecampAPIEvent.OK,
		BasecampAPIEventHandlers.handleEditOKEvent);
		
		if(abstractWrapper is IStandardWrapper)
			(abstractWrapper as IStandardWrapper).edit(node.@id, 
			node.parent().@projectId);
		else (abstractWrapper as CommentWrapper).edit(node.@id);
	}
}

/**
 * @private
 * 
 * Called when a user clicks on 'Delete' for a specific item.
 */
private function handleDeleteClickEvent(e:MouseEvent):void
{
	// Example of how to use some of the dynamic call stuff to address
	// summit resources - operations - directly
	var entry:XML = (State.tree.selectedItem as XML);
	
	var abstractWrapper:AbstractWrapper;
	abstractWrapper = Helpers.getClass(entry.@resource);
	
	Helpers.registerCommonEvents(abstractWrapper.loader);
	
	abstractWrapper.loader.addEventListener(BasecampAPIEvent.OK, 
	BasecampAPIEventHandlers.handleDestroyOKEvent);
	
	var stdWrapper:IStandardWrapper = abstractWrapper as IStandardWrapper;
	
	if(stdWrapper) stdWrapper.destroy(entry.@id);
	else (abstractWrapper as CommentWrapper).destroy(entry.@id);
}
//////////////////////////////////////////////////////// gmarius@tyemill.com //